<template>
        <div class="bjd_tk yd_tk">
            <div class="close" @click="close">
                    <a-icon type="close" />
            </div>
            <div class="tilte_box">
                <div class="bjd_tit">
                    <span class="cz_title">添加运单</span>
                    <span class="cz_title_border"></span>
                </div>
            </div>
            <div class="addyd_box">
                    <div class="list1">
                        <span class="xsdh">销售单号：&nbsp;&nbsp;{{xsdh}}</span>
                    </div>
                    <div class="list1">
                        <span class="sele_tilte"><span style="color:red">*</span>收&nbsp;货&nbsp;<span style="display:inline-block;width:5px"></span>人：</span>
                        <a-input   class="sele_input"  v-model="shrData"  @change="onShrInput" placeholder="请输入收货人"  />
                    </div>
                    <div class="list1">
                        <span class="sele_tilte"><span style="color:red">*</span>手机号码：</span>
                        <a-input   class="sele_input" v-model="phone"    @change="onPhoneInput" placeholder="请输入手机号码" />
                    </div>
                    <div class="list1">
                        <span class="sele_tilte"><span style="color:red">*</span>运输方式：</span>
                        <a-select  v-model="ysfs_val"    class="sele_input" @change="handYsfsChange" placeholder="请选择运输方式"  >
                        <a-select-option :value="ysfs" v-for="(ysfs,i) in ysfsList " :key="i" >
                            {{ysfs}}
                        </a-select-option>
                        </a-select>
                    </div>
                    <div class="list1">
                    <span class="sele_tilte"><span style="color:red">*</span> 运&nbsp;单<span style="display:inline-block;width:10px"></span>号：</span>
                        <a-input   class="sele_input"  v-model="ydhao"  @change="onYdhaoInput" placeholder="请输入运单号" />
                    </div>
                    <div class="list1">
                        <span class="sele_tilte"><span style="color:red">*</span>发货日期：</span>
                        <a-date-picker style="width:86%" show-time format="YYYY-MM-DD " :defaultValue="moment(fhrq, 'YYYY-MM-DD')"    @change="onfhrqTimeChange" />
                    </div>
            </div>
            <div class="list_btn">
                <a-button  class="save_btn save_btn1" @click="SaveWayBill('Y')">保存运单</a-button>
                <a-button type="primary" class="save_btn" @click="SaveWayBill('N')"  >保存并发送短信</a-button>
            </div>
            <div class="bcloading" v-if="isSave">
                <a-spin size="large" tip="正在保存...">
                    
                </a-spin>
            </div>
        </div>
</template>
<script>
import qs from "qs";
import moment from 'moment';
export default {
   
    data(){
        return{
            moment,
            // 跳转携带参数信息
            xsdinfo:'',
            // 销售单号
            xsdh:'',
            // 单位名称
            dwmc:'',
            // 公司编码
            gsbm:'',
            // 授权id
            sqdwid:'',
            //数据库名称
            database:'ERP2_BEARING',
            // ip地址
            ip:'122.112.240.180',
            // 联系人列表
            lxrList:[],
            // 手机号列表
            phoneList:[],
            // 运输方式
            ysfsList:[],
            //运输方式值
            ysfs_val:'',
            // 收货人
            shrData:'',
            phone:'',
            ydhao:'',
            fhrq:new Date(),
            // 控制保存成功
            isSave:false

        }
    },

    methods:{
        //关闭弹出框
        close() {
            this.$store.state.tjwz = false;
        },
        // 收货人搜索触发
        onShrInput(e){
            console.log(e.target.value)
            this.shrData=e.target.value
        },
        onPhoneInput(e){
            this.phone=e.target.value
        },
         onYdhaoInput(e){
            this.ydhao=e.target.value
        },
        onfhrqTimeChange(date, dateString) {
            this.fhrq=dateString;
        },
        // 运输方式更改触发
        handYsfsChange(value){
            // console.log(value);
            this.ysfs_val=value;
        },
        getXsdinfo(){
            this.xsdinfo=this.$store.state.xsdinfo;
            this.xsdh=this.xsdinfo.XSDH;
            this.dwmc=this.xsdinfo.DWMC;
            this.gsbm=this.xsdinfo.GSBM;
        },
        // 获取运单信息
        getyundanInfo(){
            var now = new Date();
            var year = now.getFullYear();
            var month = now.getMonth() + 1;
            var day = now.getDate();
            if (month < 10) {
                month = '0' + month;
            };
            if (day < 10) {
                day = '0' + day;
            };

            var fhrq =year + '-' + month + '-' + day
            console.log(this.$store.state.bbl_sqdwid);
            if (this.$store.state.bbl_sqdwid){
                var sqid = this.$store.state.bbl_sqdwid;
            }else{
                var sqid = 7401;
            }

            if (this.$store.state.dwmc) {
                var dwmcs = this.$store.state.dwmc;
            } else {
                var dwmcs = '百联';
            }
            // 发送axios
            this.axios
            .post(
                'https://yb.bearing.cn/wxapi/smt_newWaybillData.php',
                qs.stringify({
                        xsdh:this.xsdh,
                        token:sessionStorage.getItem('smt_token'),
                    })
            ).then(res=>{
                console.log(res);
                if (res.data.code == 103) {
                    this.shrData=res.data.result.shr,
                   this.phone=res.data.result.phone,
                   this.ysfsData=res.data.result.ysfs,
                    this.ysdh=res.data.result.ysdh,
                    this.fhrq=res.data.result.fhrq
                }
            })

        },
        // 获取收货人列表
        getShrList(){
            this.axios
            .post(
                'https://yb.bearing.cn/wxapi/smt_newLxrAddList.php',
                qs.stringify({
                        gsbm:this.gsbm,
                        dwmc:this.dwmc,
                        token:sessionStorage.getItem('smt_token'),
                    })
            ).then(res=>{
                console.log(res);
                if (res.data.code == 103) {
                    for (var ii = 0; ii < res.data.result.length; ii++) {
                        this.lxrList.push(res.data.result[ii].lxr);
                        this.phoneList.push(res.data.result[ii].phone)
                    }
                }
            })
        },
        // 获取运输方式列表
        getYsfsList(){
            this.axios
            .post(
                'https://yb.bearing.cn/wxapi/smt_newKhSort.php',
                qs.stringify({
                        gsbm:this.gsbm,
                        sortType: 3,
                        token:sessionStorage.getItem('smt_token'),
                    })
            ).then(res=>{
                console.log(res);
                if (res.data.code == 103) {
                    for (var ii = 0; ii < res.data.result.length; ii++) {
                        this.ysfsList.push(res.data.result[ii].MC)
                    }
                    // console.log(this.ysfsList);
                }
            })

        },
        //保存运单不发送短信
        SaveWayBill(value){
            console.log(value);
            if (value == 'Y'){
            var sendsms = 'Y';
            }else{
            var sendsms = '';
            }
            
            var xsdh = this.xsdh;
            var shrData = this.shrData;
            var phone = this.phone;
            var ysfsData = this.ysfsData;
            var ysdh = this.ysdh;
            var fhrq = this.fhrq;
            var zdr = this.zdr;
            var dwmc = this.dwmc;
            var gsbm = this.gsbm;
            var sqdwid = this.$store.state.bbl_sqdwid;
            console.log(this.shrData)
            if (this.shrData == ''){
                this.$notification["error"]({
                    message: "警告",
                    description: "收货人不得为空"
              });

            return false;
            }

            if (this.phone == '') {
                this.$notification["error"]({
                    message: "警告",
                    description: "手机号不得为空"
              });

            return false;
            }else{
            var pattern_phone = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
            if (!pattern_phone.test(this.phone)){
                this.$notification["error"]({
                    message: "警告",
                    description: "手机号格式错误"
              });
                return false;
            }
            }

            if (this.ysfsData == '') {
                this.$notification["error"]({
                        message: "警告",
                        description: "运输方式不得为空"
                });

            return false;
            }

        

            this.isSave=true;
            //运单信息保存
            this.axios
            .post(
                "https://yb.bearing.cn/wxapi/smt_newWaybillAdd.php",
                qs.stringify({
                    sendsms: sendsms,
                    xsdh: xsdh,
                    shr:shrData,
                    phone: phone,
                    ysfs: ysfsData,
                    ysdh: ysdh,
                    fhrq: fhrq,
                    zdr:zdr,
                    dwmc: dwmc,
                    gsbm: gsbm,
                    sqdwid: sqdwid,
                    token:sessionStorage.getItem('smt_token'),
                    })

            ).then(res=>{
                this.isSave=false;
                if (res.data.code == 103 || res.data.code == 102) {
                   this.$notification["success"]({
                        message: "成功",
                        description: "运单保存成功"
                });
                   this.close();
                   }
        })
                

                //所有表单值置空
                // that.setData({
                //   shrData:'',
                //   phone:'',
                //   ysfsData:'',
                //   ysdh:'',
                //   fhrq:''
                // })
        

        },



    },
    created(){
        this.getXsdinfo();
        this.getyundanInfo();
        this.getShrList();
        this.getYsfsList();

        
    }
}
</script>

<style lang='less' >
    .yd_tk{
        .close{
            position: absolute;
            top: -3px;
            right: -13px;
            font-size: 24px;
            width: 50px;
            height: 50px;
            cursor: pointer;
        }
        .tilte_box{
                border-bottom: 0!important;
                padding: 40px 40px 10px;
                text-align: left;
                margin-bottom: 0;
                height: 87px;
                line-height: 22px;
                .cz_title{
                    display: block;
                    width: 13%;
                    color: #1890ff;
                    text-align: center;
                    height:21px;
                    }
                    .cz_title_border{
                    display: inline-block;
                    width: 13%;
                    height: 2px;
                    background-color: #1890ff;
                    }
            }
        .list_btn{
            text-align: center;
            position: absolute;
            bottom: 20px;
            left: 50%;
            -webkit-transform: translateX(-50%);
            transform: translateX(-50%);
        }  
    }
    .bjd_tk {
        position: absolute;
        top: 5%;
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);
        width: 57%;
        height: 600px;
        background: #fff;
                .ts{
                    margin:5px 0 0 75px;
                    color:red;
                    font-size: 12px;
                    i{
                        margin: 0 5px;
                    }
                }
                .topList{
                    background-color: #fff;
                    margin-top: 0;
                    padding-left:20px;
                    span{
                        display: inline-block;
                        width: 300px;
                        text-align: left;
                    }
                }
                .addyd_box{
                    background-color: #fff;
                    padding:0 20px;
                }
                
                .list1 {
                    text-align: left;
                    .sele_tilte{
                        display: inline-block;
                        width: 12%;
                        text-align: center;
                    }
                    .xsdh{
                        display: inline-block;
                        width: 300px;
                        margin-right: 80px;
                        padding-left: 15px;
                    }
                    .sele_input{
                        width: 86%;
                    }
                    .tit {
                        display: inline-block;
                        width: 80px;
                        text-align: right;

                        i {
                            color: #ff0000;
                            padding: 0 4px;
                        }
                    }

                    .radio {
                        margin-left: 10px;
                    }

                    .text {
                        width: 288px;
                        vertical-align: top;
                    }


                }
                .bcloading{
                    position: relative;
                    left: 42%;
                    bottom: 214px;
                }
                .list_btn {
                    text-align: center;
                    .save_btn{
                        width: 130px;
                    }
                    .save_btn1{
                        
                        width: 130px;
                        margin-right:45px;
                        background: #1dd0f9;
                        color: #fff;
                        border: 1px solid #1dd0f9;
                    }
                    button {
                        width: 200px;
                        height: 40px;
                    }
        }
    }
</style>
